﻿using Rhino;
using Rhino.DocObjects;
using Rhino.Input;
using Rhino.Input.Custom;
using System.Collections.Generic;
using System.Linq;


namespace YangTools.RhinoHelper
{
    public class Selection
    {
        // GetObjects
        /// <summary>
        /// 从Rhino中选择物体
        /// </summary>
        /// <returns></returns>
        public static List<RhinoObject> GetObjects()
        {
            return GetObjects(null);
        }
        /// <summary>
        /// 从Rhino中选择物体
        /// </summary>
        /// <param name="message">命令行提示信息</param>
        /// <returns></returns>
        public static List<RhinoObject> GetObjects(string message)
        {
            return GetObjects(message, ObjectType.AnyObject, 1, 0);
        }
        /// <summary>
        /// 从Rhino中选择物体
        /// </summary>
        /// <param name="message">命令行提示信息</param>
        /// <param name="filter">要选取的物体的类型</param>
        /// <param name="minimumNumber">获取物体的最少个数</param>
        /// <param name="maximumNumber">
        /// 获取物体的最大个数
        /// 如果是 0 ：只有用户回车确认才会结束选择
        /// 如果是 -1：当用户选择的物体个数到达minimumNumber后将自动结束
        /// 如果 >0 ：当用户选择的物体个数达到这个数后自动结束
        /// </param>
        /// <returns>List<RhinoObject> 由RhinoObject组成的集合，如果没有选中物体，将返回一个个数为0的空集合</returns>
        public static List<RhinoObject> GetObjects(string message, ObjectType filter, int minimumNumber, int maximumNumber)
        {
            return GetObjects(message, filter, minimumNumber, maximumNumber, true, false, null);
        }
        /// <summary>
        /// 从犀牛中获取物体
        /// </summary>
        /// <param name="message">命令行提示信息</param>
        /// <param name="filter">要选取的物体的类型</param>
        /// <param name="minimumNumber">获取物体的最少个数</param>
        /// <param name="maximumNumber">
        /// 获取物体的最大个数
        /// 如果是 0 ：只有用户回车确认才会结束选择
        /// 如果是 -1：当用户选择的物体个数到达minimumNumber后将自动结束
        /// 如果 >0 ：当用户选择的物体个数达到这个数后自动结束
        /// </param>
        /// <param name="selectSubObjects">是否选取子物体（面的边缘，物体的子面等，不能用来控制群组的子物体）</param>
        /// <returns>List<RhinoObject> 由RhinoObject组成的集合，如果没有选中物体，将返回一个个数为0的空集合</returns>
        public static List<RhinoObject> GetObjects(string message, ObjectType filter, int minimumNumber, int maximumNumber, bool preselect, bool selectSubObjects, GetObjectGeometryFilter customFilter)
        {
            List<RhinoObject> rhinoObjects = new List<RhinoObject>();

            if (!preselect) RhinoDoc.ActiveDoc.Objects.UnselectAll();


            GetObject getObjects = new GetObject();
            if (message != null) getObjects.SetCommandPrompt(message); // 设置命令行提示字符
            getObjects.GeometryFilter = filter;
            getObjects.SubObjectSelect = selectSubObjects;
            if (customFilter != null) getObjects.SetCustomGeometryFilter(customFilter);

            // 使用 GetMultiple 而不是 Get 可以允许用户选择多个物体，并且可以预选
            var getResult = getObjects.GetMultiple(minimumNumber, maximumNumber);

            if (getResult != GetResult.Object) return rhinoObjects;

            foreach (var item in getObjects.Objects())
            {
                rhinoObjects.Add(item.Object());
            }
            getObjects.Dispose();
            return rhinoObjects;
        }


        // GetBlock
        /// <summary>
        /// 从Rhino中选择图块引例
        /// </summary>
        /// <param name="message"></param>
        /// <param name="minimumNumber"></param>
        /// <param name="maximumNumber"></param>
        /// <returns></returns>
        public static List<InstanceObject> GetBlocks(string message, int minimumNumber, int maximumNumber)
        {
            return GetObjects(message, ObjectType.InstanceDefinition|ObjectType.InstanceReference, minimumNumber, maximumNumber)
                    .Select(i=> i as InstanceObject)
                    .ToList();
        }
    }
}
